﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MineLib;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace MineLibTest
{
    class Test
    {
        static void Main(string[] args)
        {
            string error, errorCause, errorMessage;
            Console.Clear();
            Console.Write("Enter username: ");
            string user = Console.ReadLine();
            Console.Write("\nEnter password: ");
            string pass = Console.ReadLine();
            string authJSON = MCLib.Authenticate(user, pass);
            Console.WriteLine(authJSON);
            JObject authJ = JObject.Parse(authJSON);
            bool isError = MCLib.FindErrors(authJSON, out error, out errorMessage, out errorCause);
            if (isError)
            {
                Console.WriteLine("Error: {0}\nError message: {1}\nError cause: {2}", error, errorMessage, errorCause);
            }
            else
            {
                string clientToken, accessToken;
                MCLib.ReturnTokens(authJSON, out clientToken, out accessToken);
                Console.WriteLine("Client token = {0}\nAccess token = {1}", clientToken, accessToken);
                Console.WriteLine("Press any key to continue to refreshing..");
                Console.ReadKey();
                string refreshJSON = MCLib.Refresh(accessToken, clientToken);
                MCLib.ReturnTokens(refreshJSON, out clientToken, out accessToken);
                Console.WriteLine("Refreshed client token: {0}\nRefreshed access token: {1}", clientToken, accessToken);
                Console.WriteLine("\nPress any key to continue to signing out");
                Console.ReadKey();
                MCLib.Signout(clientToken, accessToken);
                Console.WriteLine("Signed out successfully!");
                Console.WriteLine("Press any key to continue..");
            }
            Console.ReadKey();
        }
    }
}
